<?php
namespace Mentors\Controller;

class MenteesController extends MainController {

    public function index(){
 
        $cate = I('get.cate');
        $map['show']=1;
        $train = M('TrainCate')->field('id,name')->where($map)->order('sort asc,id asc')->select();
        $this->assign('train',$train);
        $this->assign('cate',$cate);

        unset($map);
        if ($cate) {
            $map['trainID'] = $cate;
        }
        $map['status']=1;
        $obj = M('Needs');
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 9);
        $page->setConfig('prev',L('_prev_page'));
        $page->setConfig('next',L('_next_page'));
        $page->setConfig('first',L('_first_page'));
        $page->setConfig('end',L('_end_page'));
        $page->setConfig('theme','%first% %upPage% %linkPage% %downPage% %end%');
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select(); 
        foreach ($list as $key => $value) {
            $list[$key]['user']=M("Mentees")->where("userid=".$value['userID']."")->find();
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        
        $this->display();
    }

    public function learning(){
        $status = I('get.status');
        $obj = M('Train');
        $map['mentorsID']=$this->userID;
        $map['status']=0;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 12);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        
        foreach ($list as $key => $value) {
            $user = M('Mentees')->where('userid='.$value['menteesID'])->find();
            $list[$key]['user']=$user;
        }

        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }  

    public function graduation(){
        $obj = M('Train');
        $map['mentorsID']=$this->userID;
        $map['status']=1;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 12);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $user = M('Mentees')->where('userid='.$value['menteesID'])->find();
            $list[$key]['user']=$user;
        }

        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    } 

    public function detail(){
        $id = I('get.id');
        if(empty($id)){
            $this->error("Illegal access");
        }else{
            $map['userid']=$id;
            $list = M('Mentees')->where($map)->find();
            if (!$list) {
                $this->error('The information does not exist');
            }

            if(time()-$list['last_action_time']>60*20){
                $list['isOnline']=0;
            }else{
                $list['isOnline']=1;
            }

            $list['star']=floor(M("Comment")->where("toID=".$id."")->avg("star"));

            $list['trainItem'] = M("Needs")->where("userID=".$list['userid']." and status=1")->getField("trainItem",true);
            $this->assign('list',$list);

            $this->display();
        }
    } 

    //收徒申请
    public function apply(){
        $nid=I("nid");
        $needs=M("Needs")->where("id=".$nid."")->find();
        if(!$needs){
            echo $this->echo_json_str('ERROR',L("_public_noinfo"));die;
        }else{
             $user=M("Mentors")->where("userid=".$this->userID."")->find();
             $data['fromID']=$this->userID;
             $data['fromName'] = $user['nickname'];
             $data['fromFace'] = $user['face'];

             $data['toID']=$needs['userID'];
             $data['toName']=$needs['userName'];
             $data['toFace']=$needs['userFace'];
             $data['trainItem']=$needs['trainItem'];
             $data['skills']=$needs['skills'];
             $data['target']=$needs['target'];
             $data['train']=$needs['train'];
             $data['work']=$needs['work'];
             $data['createTime'] = time(); 
             $data['updateTime'] = time(); 
             $list = M('Apprentice')->add($data);
             if ($list) {
                 echo $this->echo_json_str("SUCCESS",L('_action_success'));die;
             }else{
                 echo $this->echo_json_str("ERROR",L('_action_fail'));die;
             }
        }
    }

    //查看学员需求详情
    public function viewneed(){
        $id = I('get.id');
        if(empty($id)){
            $this->error(L('_sys_error'));
        }else{
            $map['id']=$id;
            $list = M('Needs')->where($map)->find();
            if (!$list) {
                $this->error(L('_public_noinfo'));
            }
            $this->assign('list',$list);
            $this->display("Need_view");
        } 
    }

    //学员结业操作
    public function graduate(){
       $id = I('get.id');
       if(M("Train")->where("id=".$id."")->setField("status",1)){
         echo $this->echo_json_str("SUCCESS",L('_action_success'),U('Mentees/learning'));die;
       }else{
         echo $this->echo_json_str("ERROR",L('_action_fail'));die;
       }
    }

    //给学员评论
    public function comment(){
        if($_POST){
            $toID = I('post.toID');
            $mentors = M('Mentees')->where('userid='.$toID)->find();
            if (!$mentors) {
                echo $this->echo_json_str("ERROR",L('_not_exist'));die;
            }
            $list = M('Mentors')->where('userid='.$this->userID)->find();
            $data['fromID']=$this->userID;
            $data['fromName'] = $list['nickname'];
            $data['fromFace'] = $list['face'];
            $data['toID']=$toID;
            $data['toName']=$mentors['nickname'];
            $data['toFace']=$mentors['face'];
            $data['star']=I('post.star');
            $data['content']=I('post.content');
            $data['createTime'] = time(); 
            $list = M('Comment')->add($data);
            if ($list) {
                echo $this->echo_json_str("SUCCESS",L('_action_success'));die;
            }else{
                echo $this->echo_json_str(L('_action_fail'));die;
            }
        }else{
            $toid=I("toID");
            $map['userid']=$toid;
            $list = M('Mentees')->where($map)->find();
            $this->assign("list",$list);

            unset($map);
            $map['toID']=$toid;
            $map['fromID']=$this->userID;
            $comment=M("Comment")->where($map)->find();
            $this->assign("comment",$comment);
            $this->display();  
        }
    }

    //获取评论列表
    public function getComment(){
        $obj = M('Comment');
        $map['toID']=I("id");
        $map['status']=1;
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
           $list[$key]['createTime']=mdate($value['createTime']);
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }
}